package controller;

import entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import relationEntity.MemberUser;
import relationEntity.UserMember;
import service.GroupService;
import service.MemberService;
import service.UserService;
import util.Page;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by jifeng on 2016/12/2 0002.
 */
@Controller
public class UserController {
    @Resource(name = "userService")
    private UserService service;
    @Resource(name = "groupService")
    private GroupService groupService;
    @Resource(name ="memberService")
    private MemberService memberService;


    /**
     * Created by jifeng
     * 验证登录
     * */
    @RequestMapping("/checkLogin")
    public ModelAndView index(HttpServletRequest request,HttpServletResponse response){
        ModelAndView mv = new ModelAndView();
        String loginName= request.getParameter("loginName");
        String loginPassword=request.getParameter("loginPassword");
        List<Personnel> personnelList = service.findPersonnel(loginName,loginPassword);
        if (personnelList.size() == 0){
            int status=0;
            mv.addObject("status",status);
        }
        if (personnelList.size()!= 0){
            int status=1;
            request.getSession().setAttribute("userName", loginName);
            List<Personnel> personnelList1 =service.findPersonnelByName(loginName);
            Role role = service.findRole(personnelList1.get(0).getRoleId());
            if (role.getProductList()!= null){
                List<Menu> menuListP = service.menuList(role.getProductList());
                request.getSession().setAttribute("menuListP",menuListP);
                request.getSession().setAttribute("type",1);
            }else {
                request.getSession().setAttribute("type",0);
            }
            if (role.getUserList()!= null){
                List<Menu> menuListU = service.menuList(role.getUserList());
                request.getSession().setAttribute("menuListU",menuListU);
                request.getSession().setAttribute("type1",1);
            }else {
                request.getSession().setAttribute("type1",0);
            }
            if (role.getOrderList()!= null){
                List<Menu> menuListO = service.menuList(role.getOrderList());
                request.getSession().setAttribute("menuListO",menuListO);
                request.getSession().setAttribute("type2",1);
            }else {
                request.getSession().setAttribute("type2",0);
            }
            if (role.getSystemList()!= null){
                List<Menu> menuListS = service.menuList(role.getSystemList());
                request.getSession().setAttribute("menuListS",menuListS);
                request.getSession().setAttribute("type3",1);
            }else {
                request.getSession().setAttribute("type3",0);
            }
            if (role.getAdvertisement()!=null){
                List<Menu> menuListA = service.menuList(role.getAdvertisement());
                request.getSession().setAttribute("menuListA",menuListA);
                request.getSession().setAttribute("type4",1);
            }else {
                request.getSession().setAttribute("type4",0);
            }
            if (role.getComment() != null){
                List<Menu> menuListC = service.menuList(role.getComment());
                request.getSession().setAttribute("menuListC",menuListC);
                request.getSession().setAttribute("type5",1);
            }else {
                request.getSession().setAttribute("type5",0);
            }
            mv.addObject("status",status);
        }
        mv.setViewName("index");
        return mv;
    }


    /**
     * Created by jifeng
     * 跳转添加会员用户页面
     * */
    @RequestMapping(value = "/toAddUser")
    public ModelAndView toAddUser(){
        ModelAndView mv = new ModelAndView();
        List<MemberGroup> memberGroupList = memberService.findMemberGroupList();
        mv.addObject("memberGroupList",memberGroupList);
        mv.setViewName("addMember");
        return mv;
    }



    /**
     * Created by jifeng
     * 添加会员用户
     * */
   @RequestMapping(value = "/addUser")
   public String addUser(String name,String gender,String member,String phone,String email){
       long time = System.currentTimeMillis();
       String head ="kaa";
       ModelAndView mv =new ModelAndView();
       User user = new User();
       String userName = head + time;
       String token = time + phone;
       user.setName(name);
       user.setGender(gender);
       user.setPhone(phone);
       user.setEmail(email);
       user.setUserName(userName);
       user.setToken(token);
       user.setOrderNum(0);
       user.setDeletes(0);
       user.setSource("wap");
       service.addUser(user);//添加用户表
       Member member1 = new Member();
       String memberCode = head + time + user.getId();
       member1.setMemberCode(memberCode);
       member1.setLevel(member);
       member1.setIntegral(0);
       member1.setUserId(user.getId());
       memberService.addMember(member1);//添加会员表
       mv.addObject("user",user);
       mv.addObject("member",member1);
       return "redirect:/userList";
   }

    /**
     * Created by jifeng
     * 查询会员列表
     * */
    @RequestMapping(value = "/userList")
    public Model userList(HttpServletRequest request, HttpServletResponse response,Model mv){
        try {
            String pageNo = request.getParameter("pageNo");
            if (pageNo == null) {
                pageNo = "1";
            }
            String date = request.getParameter("date");
            String source =request.getParameter("source");
            String level =request.getParameter("level");
            String userName = request.getParameter("userName");
            Page page =service.findUserList(Integer.valueOf(pageNo),date,source,level,userName, 10);
            List<MemberGroup> memberGroupList = memberService.findMemberGroupList();
            mv.addAttribute("page",page);
            if (date ==""){
                date=null;
            }
            mv.addAttribute("date",date);
            mv.addAttribute("source",source);
            mv.addAttribute("level",level);
            mv.addAttribute("userNames",userName);
            mv.addAttribute("memberGroupList",memberGroupList);
            List<UserMember> userMemberList = page.getList();
            mv.addAttribute("userMemberList",userMemberList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * Created by jifeng
     * 批量删除，伪删除只是改变状态
     * */
    @RequestMapping(value = "/deleteUser")
    public String deleteUser(String ids){
        if (ids!=null&&ids!="") {
            service.deleteUsers(ids);
        }
        return "redirect:/userList";
    }

    /**
     * Created by jifeng
     * user回收站用于查看被删除的用户delete=1
     * */
    @RequestMapping(value = "/recycleUserList")
    public Model recycleUserList(HttpServletRequest request,HttpServletResponse response,Model mv){
        try {
            String pageNo = request.getParameter("pageNo");
            if (pageNo == null) {
                pageNo = "1";
            }
            String date = request.getParameter("date");
            String source =request.getParameter("source");
            String level =request.getParameter("level");
            String userName = request.getParameter("userName");
            Page page =service.recycleUserList(Integer.valueOf(pageNo),date,source,level,userName, 10);
            List<MemberGroup> memberGroupList = memberService.findMemberGroupList();
            mv.addAttribute("page",page);
            if (date ==""){
                date=null;
            }
            mv.addAttribute("date",date);
            mv.addAttribute("source",source);
            mv.addAttribute("level",level);
            mv.addAttribute("userName",userName);
            mv.addAttribute("memberGroupList",memberGroupList);
            List<UserMember> userMemberList = page.getList();
            mv.addAttribute("userMemberList",userMemberList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }


    /**
     * Created by jifeng
     * 批量恢复，改变状态为0
     * */
    @RequestMapping(value = "/recycleUser")
    public String recycleUser(String ids){
        if (ids!=null&&ids!="") {
            service.recycleUser(ids);
        }
        return "redirect:/userList";
    }


    /**
     * Created by jifeng
     * 积分管理列表
     * */
    @RequestMapping(value = "/integralList")
    public Model integralList(HttpServletRequest request,HttpServletResponse response,Model mv){
        try {
            String pageNo = request.getParameter("pageNo");
            if (pageNo == null){
                pageNo = "1";
            }
            String date = request.getParameter("date");
            String integral =request.getParameter("integral");
            String level =request.getParameter("level");
            String userName = request.getParameter("userName");
            Page page = service.IntegralManagementList(Integer.valueOf(pageNo),date,integral,level,userName,10);
            List<MemberGroup> memberGroupList = memberService.findMemberGroupList();
            mv.addAttribute("page",page);
            if (date ==""){
                date=null;
            }
            mv.addAttribute("date",date);
            mv.addAttribute("integral",integral);
            mv.addAttribute("level",level);
            mv.addAttribute("userNames",userName);
            mv.addAttribute("memberGroupList",memberGroupList);
            List<MemberUser> memberUserList = page.getList();
            mv.addAttribute("memberUserList",memberUserList);
        }catch (RuntimeException er){
            throw er;
        }
        return mv;
    }


    /**
     * Created by jifeng
     * 积分清零
     * */
    @RequestMapping(value = "/deleteIntegral")
   public String deleteIntegral(HttpServletRequest request,HttpServletResponse response){
       String ids = request.getParameter("ids");
       if (ids!=null&&ids!="") {
           service.deleteIntegral(ids);
       }
       return "redirect:/integralList";
   }

    /**
     * Created by jifeng
     * 会员组列表，显示会员分类级别
     * */
    @RequestMapping(value = "/memberGroup")
    public Model memberGroup(HttpServletRequest request,HttpServletResponse response,Model mv){
        List<MemberGroup> memberGroup = memberService.findMemberGroupList();
        mv.addAttribute("memberGroup", memberGroup);
        return mv;
    }

    /**
     * Created by jifeng
     * 删除会员类型
     * */
    @RequestMapping(value = "/deleteMemberGroup")
    public String deleteMemberGroup(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        memberService.deleteMemberGroup(id);
        return "redirect:/memberGroupList";
    }


    /**
     * Created by jifeng
     * 充值积分
     * */
    @RequestMapping(value = "/updateMember")
    public String updateMember(HttpServletRequest request,HttpServletResponse response){
        long integral = Long.parseLong(request.getParameter("integral"));
        int id = Integer.parseInt(request.getParameter("integralId"));
        Member member = memberService.findMemberById(id);
        member.setIntegral(integral);
        memberService.updateMember(member);
        return "redirect:/integralList";
    }

    /**
     * Created by jifeng
     * 添加会员类型
     * */
    @RequestMapping(value = "/addMemberGroup")
    public String addMemberGroup(HttpServletRequest request,HttpServletResponse response){
        long start = Long.parseLong(request.getParameter("start"));
        long end =Long.parseLong(request.getParameter("end"));
        String memberLevel = request.getParameter("memberLevel");
        MemberGroup memberGroup = new MemberGroup();
        memberGroup.setStart(start);
        memberGroup.setEnd(end);
        memberGroup.setMemberLevel(memberLevel);
        memberService.addMemberGroup(memberGroup);
        return "redirect:/memberGroupList";
    }

    /**
     * Created by jifeng
     * 跳转user编辑
     * */
    @RequestMapping(value = "/memberEdit")
    public Model memberEdit(HttpServletRequest request,HttpServletResponse response,Model mv){
        String id= request.getParameter("userId");
        UserMember userMember = service.findUserMemberById(Integer.valueOf(id));
        mv.addAttribute("userMember",userMember);
        return mv;
    }

    /**
     * Created by jifeng
     * 跳转user编辑
     * */
    @RequestMapping(value = "/updateUserMember")
    public String updateUserMember(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        String userName=request.getParameter("userName");
        String level = request.getParameter("level");
        String name = request.getParameter("name");
        String gender= request.getParameter("gender");
        String phone= request.getParameter("phone");
        String email = request.getParameter("email");
        service.updateUserM(Integer.valueOf(id),userName,name,gender,phone,email);
        memberService.updateMemberU(Integer.valueOf(id),level);
        return "redirect:/userList";
    }

}
